package com.wanxi.service.impl;

import com.wanxi.dao.CuisineDao;
import com.wanxi.dao.impl.CuisineDaoImpl;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.CompanyModel;
import com.wanxi.model.CuisineModel;
import com.wanxi.service.CuisineService;
import com.wanxi.util.JSONCopy;
import com.wanxi.util.Tool;
import com.wanxi.vo.CuisineVO;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class CuisineServiceImpl implements CuisineService {
    private CuisineDao cuisineDao = new CuisineDaoImpl();

    @Override
    public ResultDTO add(CuisineVO cuisineVO) {
        /**
         * 1.将vo里面的值复制到model：vo只存在于servlet和service
         * 2.调用dao层得到结果
         * 3.将结果封装到dto
         * 4.将dto返回
         */
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        CuisineDao cuisineDao = new CuisineDaoImpl();
        int count = cuisineDao.add(cuisineModel);
//        ResultDTO resultDTO = ResultDTO.builder()
//                .count(count)
//                .build();
//        return resultDTO;
        return ResultDTO.getDTO(count, null);
    }

    @Override
    public ResultDTO update(CuisineVO cuisineVO) {
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        CuisineDao cuisineDao = new CuisineDaoImpl();
        int count = cuisineDao.update(cuisineModel);
//        ResultDTO resultDTO = ResultDTO.builder()
//                .count(count)
//                .build();
//        return resultDTO;
        Jedis jedis = new Jedis();
        if (count > 0) {
            jedis.del("cuisineName", "cuisineId");
        }
        return ResultDTO.getDTO(count, null);
    }

    @Override
    public ResultDTO delete(CuisineVO cuisineVO) {
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        CuisineDao cuisineDao = new CuisineDaoImpl();
        int count = cuisineDao.delete(cuisineModel);
//        ResultDTO resultDTO = ResultDTO.builder()
//                .count(count)
//                .build();
//        return resultDTO;
        return ResultDTO.getDTO(count, null);
    }

    @Override
    public ResultDTO findById(CuisineVO cuisineVO) {
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        CuisineDao cuisineDao = new CuisineDaoImpl();
        CuisineModel model = cuisineDao.findById(cuisineModel);
//        ResultDTO resultDTO = ResultDTO.builder()
//                .data(model)
//                .code(0)
//                .build();
//        return resultDTO;
        return ResultDTO.getDTO(0, model);
    }

    @Override
    public ResultDTO findAll(CuisineVO cuisineVO) {
        /**
         * 要成功复制，则必须满足VO和model里面的属性名一致
         */
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        CuisineDao cuisineDao = new CuisineDaoImpl();
        List<CuisineModel> cuisineModelList = cuisineDao.findAll(cuisineModel);
        int count = cuisineDao.count(cuisineModel);
//        ResultDTO resultDTO = ResultDTO.builder()
//                .data(cuisineModelList)
//                .code(0)
//                .count(count)
//                .build();
//        return resultDTO;
        return ResultDTO.getDTO(count, cuisineModelList);
    }

    @Override
    public ResultDTO count(CuisineVO cuisineVO) {
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        CuisineDao cuisineDao = new CuisineDaoImpl();
        int count = cuisineDao.count(cuisineModel);
//        ResultDTO resultDTO = ResultDTO.builder()
//                .code(0)
//                .count(count)
//                .build();
//        return resultDTO;
        return ResultDTO.getDTO(count, null);
    }

    @Override
    public List<CuisineModel> getCuisineModelList() {
        List<CuisineModel> cuisineModelList = new ArrayList<>();
        Jedis jedis = new Jedis();
        boolean isHave = jedis.exists("cuisineName");
        if (isHave) {
            List<String> cuisineNameList = jedis.lrange("cuisineName", 0, jedis.llen("cuisineName"));
            List<String> cuisineIdList = jedis.lrange("cuisineId", 0, -1);
            for (int i = 0; i < cuisineNameList.size(); i++) {
                CuisineModel model = new CuisineModel();
                model.setId(Tool.strToInt(cuisineIdList.get(i)));
                model.setName(cuisineNameList.get(i));
                cuisineModelList.add(model);
            }
        } else {
            CuisineModel model = new CuisineModel();
            model.setEnable(1);
            model.setPage(1);
            model.setLimit(10);
            model.setName("");
            cuisineModelList = cuisineDao.findAll(model);
            for (CuisineModel m : cuisineModelList
            ) {
                jedis.rpush("cuisineName", m.getName());
                jedis.rpush("cuisineId", m.getId() + "");
            }

        }
        return cuisineModelList;
    }

//    private ResultDTO getDTO(Integer count) {
//        ResultDTO resultDTO = ResultDTO.builder()
//                .code(0)
//                .count(count)
//                .build();
//        return resultDTO;
//    }


}
